﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Aliyeye.Car.Net.UI.Components;
using Aliyeye.Car.Net.UI.PopupDialog;
using Aliyeye.Car.Net.UI.Common;

namespace Aliyeye.Car.Net.UI.SystemModel
{
    public partial class Role : Aliyeye.Car.Net.UI.BaseForm
    {
        Accounts_RolesSvc.SoapHeaderClass sopaHeader = new Accounts_RolesSvc.SoapHeaderClass();
        private String strID = "";

        public Role()
        {
            InitializeComponent();
        }

        private void Role_Load(object sender, EventArgs e)
        {
            //BLL.Accounts_Roles bll = new BLL.Accounts_Roles();
            //dataGridView1.DataSource = bll.GetList().Tables[0];

            this.pager1.PageCurrent = 1;
            this.pager1.Bind();

        }

        private int dgvBind()
        {
            string _strSql = "";
            string strWhere = "";
            PageData pageData = new PageData(this,pager1,overlay);
            pageData.TableName = "Accounts_Roles";
            pageData.PrimaryKey = "RoleID";
            pageData.OrderStr = "RoleID desc";
            pageData.PageIndex = this.pager1.PageCurrent;
            pageData.PageSize = this.pager1.PageSize;
            pageData.QueryCondition = _strSql + strWhere.ToString();
            pageData.QueryFieldName = "*";

            DataSet ds = pageData.QueryDataTable();
            dgvInfo.AutoGenerateColumns = false;
            //if (ds != null)
            //{
            //    this.pager1.bindingSource.DataSource = ds.Tables[0];
            //    this.pager1.bindingNavigator.BindingSource = pager1.bindingSource;
            //}
            dgvInfo.DataSource = this.pager1.bindingSource;
            return pageData.TotalCount;
        }

        private int pager1_EventPaging(EventPagingArg e)
        {
            return dgvBind();
        }

        //添加角色
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtRoleName.Text))
            {
                DialogWarning dia = new DialogWarning("角色名称不能为空！");
                dia.ShowDialog();
                return;
            }
            overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;

            Accounts_RolesSvc.Accounts_Roles model = new Accounts_RolesSvc.Accounts_Roles();
            model.Description = txtRoleName.Text;
            //BLL.Accounts_Roles bll = new BLL.Accounts_Roles();

            //添加
            if (strID == "")
            {
                ServiceManipulator.SafeAsyncExecution<Accounts_RolesSvc.Accounts_RolesServiceSoapClient>(
                    proxy => proxy.Add(sopaHeader, model),
                    result =>
                    {
                        if (result.Error == null)
                        {
                            PageCommon.Notify("添加角色成功！");
                            overlay.FadeOut();Cursor.Current = Cursors.Default;
                            btnAdd.Text = "添加";
                            txtRoleName.Text = "";
                            strID = "";
                            this.pager1.Bind();
                        }
                        else
                        {
                            PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                        }
                    },
                    this
                );
            }
            else
            {
                //修改
                model.RoleID = Convert.ToInt32(strID);

                ServiceManipulator.SafeAsyncExecution<Accounts_RolesSvc.Accounts_RolesServiceSoapClient>(
                    proxy => proxy.Update(sopaHeader, model),
                    result =>
                    {
                        if (result.Error == null)
                        {
                            PageCommon.Notify("修改角色成功！");
                            overlay.FadeOut();Cursor.Current = Cursors.Default;
                            btnAdd.Text = "添加";
                            txtRoleName.Text = "";
                            strID = "";
                            this.pager1.Bind();
                        }
                        else
                        {
                            PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                        }
                    },
                    this
                );
            }
           
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (IsANonHeaderButtonCell(e))
            {
                ButtonClicked(e);
            }
        }

        private void ButtonClicked(DataGridViewCellEventArgs e)
        {
            String strButtonContent = dgvInfo.Columns[e.ColumnIndex].Name;
            String id = dgvInfo.Rows[e.RowIndex].Cells["编号"].Value.ToString();
            String name = dgvInfo.Rows[e.RowIndex].Cells["角色名称"].Value.ToString();
            if (name.Trim() == "客户" || name.Trim() == "驾驶员" || name.Trim() == "系统管理员")
            {
                PageCommon.Alert("你选择的角色不能操作。");
                return;
            }
            if (strButtonContent.Trim().Equals("修改"))
            {
                this.txtRoleName.Text = name;
                strID = id;
                btnAdd.Text = "保存";
                btnAdd.Refresh();
                btnCancel.Enabled = true;
            }
            else if (strButtonContent.Trim().Equals("删除"))
            {
                //"确定删除？"
                DialogConfirm diaSecc = new DialogConfirm("确定删除此角色吗？");
                if (diaSecc.ShowDialog() == DialogResult.OK)
                {
                    overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;
                    Accounts_RolesSvc.Accounts_Roles model = new Accounts_RolesSvc.Accounts_Roles();
                    model.RoleID = Convert.ToInt32(id);

                    ServiceManipulator.SafeAsyncExecution<Accounts_RolesSvc.Accounts_RolesServiceSoapClient>(
                   proxy => proxy.Delete(sopaHeader, model),
                   result =>
                   {
                       if (result.Error == null)
                       {
                           PageCommon.Notify("删除角色成功！");
                           overlay.FadeOut();Cursor.Current = Cursors.Default;
                           btnAdd.Text = "添加";
                           btnAdd.Refresh();
                           txtRoleName.Text = "";
                           strID = "";
                           this.pager1.Bind();
                       }
                       else
                       {
                           PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                       }
                   },
                   this
               );
                }
            }
        }
        

        private bool IsANonHeaderButtonCell(DataGridViewCellEventArgs cellEvent)
        {
            if (dgvInfo.Columns[cellEvent.ColumnIndex] is
                 DataGridViewButtonColumn &&
                 cellEvent.RowIndex != -1)
            { return true; }
            else { return (false); }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            btnAdd.Text = "添加";
            btnAdd.Refresh();
            txtRoleName.Text = "";
            strID = "";
            btnCancel.Enabled = false;
        }

        private void dgvInfo_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            if (this.dgvInfo.Rows.Count != 0)
            {
                for (int i = 0; i < this.dgvInfo.Rows.Count; )
                {
                    this.dgvInfo.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.PapayaWhip;
                    i += 2;
                }
            }
        }
    }
}
